package com.dlc.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.dto.PublicBankDTO;
import com.dlc.shop.bean.model.PublicBank;
import com.dlc.shop.bean.vo.PublicBankVO;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.dao.PublicBankMapper;
import com.dlc.shop.service.PublicBankService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gaozijie
 * @date 2023-03-21
 */
@Service
@AllArgsConstructor
public class PublicBankServiceImpl extends ServiceImpl<PublicBankMapper, PublicBank> implements PublicBankService  {

    private final PublicBankMapper publicBankMapper;

    @Override
    @Cacheable(cacheNames = "public_bank", key = "'list'")
    public List<PublicBankVO> listPublicBank() {
        return publicBankMapper.listPublicBank(new PublicBankDTO());
    }

    @Override
    public IPage<PublicBankVO> pagePublicBank(PageParam<PublicBank> pageParam, PublicBankDTO publicBankDTO) {
        PageParam<PublicBank> page = this.page(pageParam, new LambdaQueryWrapper<PublicBank>()
                .like(!ObjectUtils.isEmpty(publicBankDTO.getBankName()), PublicBank::getBankName, publicBankDTO.getBankName())
                .likeRight(!ObjectUtils.isEmpty(publicBankDTO.getBankCode()), PublicBank::getBankCode, publicBankDTO.getBankCode()));
        // 转成VO
        PageParam<PublicBankVO> publicBankPage = new PageParam<>();
        BeanUtils.copyProperties(page, publicBankPage);
        List<PublicBankVO> publicBankV0List = page.getRecords().stream().map(x -> {
            PublicBankVO publicBankVO = new PublicBankVO();
            BeanUtils.copyProperties(x, publicBankVO);
            return publicBankVO;
        }).collect(Collectors.toList());
        publicBankPage.setRecords(publicBankV0List);
        return publicBankPage;
    }

    @Override
    @CacheEvict(cacheNames = "public_bank", key = "'list'")
    public void save(PublicBankDTO publicBankDTO) {
        publicBankMapper.save(publicBankDTO);
    }

    @Override
    @CacheEvict(cacheNames = "public_bank", key = "'list'")
    public void update(PublicBankDTO publicBankDTO) {
        publicBankMapper.update(publicBankDTO);
    }

    @Override
    @CacheEvict(cacheNames = "public_bank", key = "'list'")
    public void deleteByIds(List<Long> bankIds) {
        publicBankMapper.deleteByIds(bankIds);
    }
}
